//
//  CZMainViewController.swift
//  微博08
//
//  Created by 1 on 16/2/28.
//  Copyright © 2016年 cjf. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController, CZMainTabBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabBar = CZMainTabBar()
        
        //（代理方法）4.设置代理
        mainTabBar.composeDelegate = self
        
        setValue(mainTabBar, forKey: "tabBar")
        
        addChildViewControllers()

    }

    // MARK: - 实现代理方法，响应按钮点击事件
    // (代理方法)5.实现代理方法
    func mainTabBarComposeClick() {
        print("在 CZMainViewController 响应到了按钮的点击事件")
    }
    
    func addChildViewControllers() {
        //首页
        let homeVC = CZHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        //消息
        let MessageVC = CZMessageViewController()
        addChildViewController(MessageVC, title: "消息", imageName: "tabbar_message_center")
        //占位控制器
        addChildViewController(UIViewController())
        //发现
        let DiscoverVC = CZDiscoverViewController()
        addChildViewController(DiscoverVC, title: "发现", imageName: "tabbar_discover")
        //我
        let ProfileVC = CZProfileViewController()
        addChildViewController(ProfileVC, title: "我", imageName: "tabbar_profile")

    }
    
    //添加tabBar的子控制器
    func addChildViewController(Controller: UIViewController, title: String, imageName: String) {
        
        Controller.title = title
        Controller.tabBarItem.image = UIImage(named: imageName)
        
        //设置选中图片
        let higetedImageName = imageName + "_highlighted"
        //设置图片不被渲染
        Controller.tabBarItem.selectedImage = UIImage(named: higetedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置选中文字的颜色
        Controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        //添加为tabBar的子控制器，并且包装导航控制器
        
        addChildViewController(UINavigationController(rootViewController: Controller))
    }

}
